package com.yuelife.admin.controller;

import com.yuelife.admin.pojo.Discussion;
import com.yuelife.admin.service.DiscussionService;
import com.yuelife.admin.util.DataGridView;
import com.yuelife.admin.util.ResultDto;
import com.yuelife.admin.vo.DiscussionVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: yangsen
 * @Date: 2020/7/2 16:28
 */
@RestController
public class DiscussionController {
    @Autowired
    DiscussionService discussionService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @InitBinder  //解决前段传过来时间的字符串解析成时间报错问题
    public void initBinder(WebDataBinder webDataBinder){
        webDataBinder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }

    @GetMapping("/discussionList")
    @ResponseBody
    public DataGridView discussionList (DiscussionVO discussionVO){
        DataGridView dataGridView = discussionService.selectAll(discussionVO);
        return dataGridView;
    }
    @PostMapping("/deleteDiscussion")
    @ResponseBody
    public ResultDto deleteDiscussion(Integer dId){

        try {
            discussionService.deleteDiscussionById(dId);
            return new ResultDto(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDto(401,"删除失败");
        }
    }
}
